//// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest
//
//import com.intellij.collaboration.util.serviceGet
//import com.intellij.openapi.components.Service
//import com.intellij.openapi.project.Project
//import git4idea.remote.hosting.SingleHostedGitRepositoryConnectionManager
//import git4idea.remote.hosting.SingleHostedGitRepositoryConnectionManagerImpl
//import git4idea.remote.hosting.ValidatingHostedGitRepositoryConnectionFactory
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.CoroutineStart
//import kotlinx.coroutines.awaitCancellation
//import kotlinx.coroutines.flow.StateFlow
//import kotlinx.coroutines.launch
//import com.linqingying.gitcode.api.GCRepositoryConnection
//import com.linqingying.gitcode.api.GitCodeApiRequestExecutor
//import com.linqingying.gitcode.authentication.accounts.GCAccountManager
//import com.linqingying.gitcode.authentication.accounts.GitCodeAccount
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContextRepository
//import com.linqingying.gitcode.util.GCGitRepositoryMapping
//import com.linqingying.gitcode.util.GCHostedRepositoriesManager
//
//@Service(Service.Level.PROJECT)
//internal class GCRepositoryConnectionManager(project: Project, parentCs: CoroutineScope) :
//  SingleHostedGitRepositoryConnectionManager<GCGitRepositoryMapping, GitCodeAccount, GCRepositoryConnection> {
//
//  private val dataContextRepository = project.serviceGet<GCPRDataContextRepository>()
//
//  private val connectionFactory =
//    ValidatingHostedGitRepositoryConnectionFactory(project.serviceGet<GCHostedRepositoriesManager>(),
//                                                   serviceGet<GCAccountManager>()) { repo, account, tokenState ->
//      createConnection(this, tokenState, repo, account)
//    }
//
//  private suspend fun createConnection(connectionScope: CoroutineScope,
//                                       tokenState: StateFlow<String>,
//                                       repo: GCGitRepositoryMapping,
//                                       account: GitCodeAccount): GCRepositoryConnection {
//    val tokenSupplier = GitCodeApiRequestExecutor.MutableTokenSupplier(account.server, tokenState.value)
//    connectionScope.launch {
//      tokenState.collect {
//        tokenSupplier.token = it
//      }
//    }
//    val executor = GitCodeApiRequestExecutor.Factory.getInstance().create(tokenSupplier)
//
//    val dataContext = dataContextRepository().getContext(repo.repository, repo.remote, account, executor)
//    connectionScope.launch(start = CoroutineStart.UNDISPATCHED) {
//      try {
//        awaitCancellation()
//      }
//      finally {
//        dataContextRepository().clearContext(repo.repository)
//      }
//    }
//    return GCRepositoryConnection(connectionScope, repo, account, dataContext)
//  }
//
//  private val delegate = SingleHostedGitRepositoryConnectionManagerImpl(parentCs, connectionFactory)
//
//  override val connectionState: StateFlow<GCRepositoryConnection?>
//    get() = delegate.connectionState
//
//  override suspend fun openConnection(repo: GCGitRepositoryMapping, account: GitCodeAccount): GCRepositoryConnection? =
//    delegate.openConnection(repo, account)
//
//  override suspend fun closeConnection() = delegate.closeConnection()
//}
